<?php
/*************************************************************************
Class Products
----------------------------------------------------------------
AvaCMS Project
Company: Avasoft Co., Ltd                                  
Name: Tran Thi Kim Que
Last updated: 08/03/2011
**************************************************************************/
include_once(ROOT_PATH."classes/dao/model.class.php");
include_once(ROOT_PATH."classes/dao/productpromotioninfo.class.php");
class ProductPromotion extends Model {
	var $table;			# Default table name
	# Constructor
	function ProductPromotion($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->table = DB_PREFIX."product_promotion";	
	}
/*-----------------------------------------------------------------------*
* Function: getObject
* Parameter: key
* Return: Info object
*-----------------------------------------------------------------------*/
	function getObject($key = '0') {
		$result = $this->select('*',"`id` = '$key'");
		if($result) {
			$productPromotionInfo = new ProductPromotionInfo (
											$result[0]['productid'],
											$result[0]['name'],
											$result[0]['content'],
											$result[0]['date_begin'],
											$result[0]['date_end'],
											$result[0]['status'],
											$result[0]['id']
											);
			return $productPromotionInfo;
		}
		return '';
	}
	

/*-----------------------------------------------------------------------*
* Function: getObjects
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjects($page = 1, $condition = '`pid` = 0', $sort = array(), $items_per_page = DEFAULT_ADMIN_ROWS_PER_PAGE) {
		if(!$page) $page = 1;
		$start = ($page -1) * $items_per_page;
		$results = $this->select('*', $condition, $sort, $start, $items_per_page);
		if($results) {
			$productPromotionInfos = array();
			foreach($results as $key => $result) {
				$productPromotionInfos[] = new ProductPromotionInfo (
											$result['productid'],
											$result['name'],
											$result['content'],
											$result['date_begin'],
											$result['date_end'],
											$result['status'],
											$result['id']
											);
			}
			return $productPromotionInfos;
		}
		return '';
	}
	function comboListObjects($pk = 'id', $title = 'vn_name', $value='', $condition = '1=1', $sort = array('position' => 'ASC')){		
		$results = $this->select("`$pk`,`$title`", $condition, $sort);
		$str = '';
		if($results){
			foreach($results as $key => $result) {
				$str .= "<option value=\"".$result[$pk]."\"".($result[$pk]==$value?" selected":"").">".$result[$title]."</option>";
			}
		}				
		return $str;
	}
	
	function addData($pro, $page=1, $items_per_page = DEFAULT_ADMIN_ROWS_PER_PAGE) {
		if(!$page) $page = 1;
		$start = ($page -1) * $items_per_page;
		$result = $this->add($pro);
		if($result) {
			return $result;
		}
		return '';
	}
	
	function updateData($pro, $mid) {
		$result = $this->update($pro, "id = '$mid' ");
		if($result) {
			return $result;
		}
		return '';
	}

	function cleanTrash() {
		$results = $this->delete("status=2");
		if ($results)
			return 1;
		return 0;
	}

}
?>